# 166. 分数到小数

var fractionToDecimal = function(numerator, denominator) {
  if (numerator % denominator === 0) return "" + numerator / denominator;

  const res = [];
  if ((numerator < 0) ^ (denominator < 0)) {
    res.push("-");
  }

  // 整数部分
  numerator = Math.abs(numerator);
  denominator = Math.abs(denominator);
  res.push(Math.floor(numerator / denominator));
  res.push(".");

  // 小数部分
  const decimal = [];

  const map = new Map();
  let surPlus = parseInt(numerator % denominator);
  let index = 0;
  while (surPlus !== 0 && !map.has(surPlus)) {
    map.set(surPlus, index);
    surPlus *= 10;
    decimal.push(Math.floor(surPlus / denominator));
    surPlus %= denominator;
    index++;
  }

  // 存在循环
  if (surPlus !== 0) {
    const index = map.get(surPlus);
    decimal.splice(index, 0, "(");
    decimal.push(")");
  }

  res.push(decimal.join(""));

  return res.join("");
};
// console.log(fractionToDecimal(1, 2));
// console.log(fractionToDecimal(2, 1));
// console.log(fractionToDecimal(2, 3));
// console.log(fractionToDecimal(4, 333));
// console.log(fractionToDecimal(1, 5));
console.log(fractionToDecimal(1, 214748364));
console.log(fractionToDecimal(-1, -2147483648));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Last Updated: 6/27/2023, 7:40:45 PM